package net.wanho.systemSettings.controller;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSONObject;
import net.wanho.commons.exception.DAOException;
import net.wanho.commons.exception.ServiceException;
import net.wanho.commons.factory.BeanFactory;
import net.wanho.commons.servlet.BaseServlet;
import net.wanho.commons.vo.AjaxResult;
import net.wanho.systemSettings.po.Department;
import net.wanho.systemSettings.po.Employee;
import net.wanho.systemSettings.po.Position;
import net.wanho.systemSettings.service.DepartmentService;
import net.wanho.systemSettings.service.EmmPositionService;
import net.wanho.systemSettings.service.EmployeeService;
import org.apache.commons.lang3.StringUtils;

@WebServlet("/EmployeeServlet")
public class EmployeeServlet extends BaseServlet<EmployeeService> {

    DepartmentService departmentService = BeanFactory.getBean(DepartmentService.class);
    EmmPositionService positionService = BeanFactory.getBean(EmmPositionService.class);
	protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		String methodName = request.getParameter("method");
		try {
			Method method = getClass().getDeclaredMethod(methodName,
					HttpServletRequest.class, HttpServletResponse.class);
			method.setAccessible(true);
			method.invoke(this, request, response);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	protected String getAllEmployee(HttpServletRequest request, HttpServletResponse response) {
        try {
            String employeeId = request.getParameter("emmployeeId");
            String employeeName = request.getParameter("emmployeename");
            //2.创建map 封装查询条件
            Map<String,String> condMap = new HashMap<>(1);
            //3. 判断条件是否有效【有效】
            if (StringUtils.isNotEmpty(employeeId)){
                //map中存放值   key ： SQL 列名   value  用户输入关键字
                condMap.put("EMPLOYEE_ID",employeeId);
            }
            if(StringUtils.isNotEmpty(employeeName))
            {
                condMap.put("EMPLOYEE_NAME",employeeName) ;
            }
            List<Employee> employeelist = super.baseService.findByCond(condMap);
            request.setAttribute("employeelist",employeelist);
            return "/WEB-INF/view/systemSettings/manage/index.jsp";
        } catch (DAOException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }

	protected String findEmployeeById(HttpServletRequest request, HttpServletResponse response) throws ServletException {
		try {
            String employeeId = request.getParameter("employeeId");
            Employee employee = super.baseService.findById(employeeId);
            request.setAttribute("employee",employee);
            //查询下拉框中的数据
            List<Department> departmentList = departmentService.findAll();
            request.setAttribute("departmentList",departmentList);
            List<Position> positionList = positionService.findAll();
            request.setAttribute("positionList",positionList);
            return "/WEB-INF/view/systemSettings/manage/update.jsp";
		} catch (ServiceException e) {
            e.printStackTrace();
            throw new ServletException(e.getMessage());
        }
    }

	protected String getDeapartAndPostionInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException {

        //request.setAttribute("", list1);
        //request.setAttribute("", list2);
        //查询下拉框中的数据
        try {
            List<Department> departmentList = departmentService.findAll();
            request.setAttribute("departmentList",departmentList);
            List<Position> positionList = positionService.findAll();
            request.setAttribute("positionList",positionList);
            return "/WEB-INF/view/systemSettings/manage/create.jsp";
        } catch (ServiceException e) {
            e.printStackTrace();
            throw new ServletException(e.getMessage());
        }
    }

	protected void checkParentEmployee(HttpServletRequest request,HttpServletResponse response) {
		JSONObject json = new JSONObject();
		try {
			
		String parentId  = 	request.getParameter("");
		Integer parentId1;
		
		
			response.getWriter().write(json.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	protected AjaxResult addEmployee(HttpServletRequest request, HttpServletResponse response) throws IOException{
		try {
            Employee employee = super.parseRequest(request, Employee.class);
            super.baseService.save(employee);
           return AjaxResult.success();
		} catch (ServiceException e) {
            e.printStackTrace();
            return AjaxResult.fail();
        }
    }

	protected AjaxResult updateEmployee(HttpServletRequest request, HttpServletResponse response) throws ServiceException {

        try {
            Employee employee = super.parseRequest(request, Employee.class);
            super.baseService.modify(employee);
            return AjaxResult.success();
        } catch (ServiceException e) {
            e.printStackTrace();
            return AjaxResult.fail();
        }
    }

	protected void updateEmployeeStatus(HttpServletRequest request,HttpServletResponse response){
		try {
            Employee employee = super.parseRequest(request, Employee.class);
            super.baseService.modifyremove(employee);
		} catch (ServiceException e) {
			e.printStackTrace();
		}
	}

}
